<?php

namespace App\Models\Archive\Migration;

use App\Models\Common\ArchiveCatalog;
use App\Models\Common\BaseMigration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Psr\Log\LoggerInterface;
use function app;

class LaneOpenLogsTable extends BaseMigration
{
    const TABLE_NAME = 'lane_open_logs';
    protected $connection = self::DB_CONN;
    const SPLIT_MODE = self::SPLIT_MODE_OPTION_YEAR;

    public function up(string $tableName): bool
    {
        $success = false;

        try {
            $conn = Schema::connection($this->connection);

            if (!$conn->hasTable($tableName)) {
                $conn->create($tableName, function (Blueprint $table) {
                    $table->uuid('id')->primary();
                    $table->string('car_no', 50)->default('')->index('idx_car_no');
                    $table->uuid('lane_id')->default('');
                    $table->string('lane_name', 50)->default('');
                    $table->string('lane_category', 10)->default('');
                    $table->uuid('user_id')->default('');
                    $table->string('user_name', 50)->default('');
                    $table->string('source', 10)->default('');
                    $table->string('pic')->default('');
                    $table->string('remark')->default('');
                    $table->tinyInteger('status');
                    $table->string('msg')->default('');
                    $table->uuid('parking_lot_id');
                    $table->timestamps();
                    $table->index(['parking_lot_id', 'car_no', 'lane_id'], 'idx_parking_lot_id');
                    $table->index('created_at', 'idx_created_at');
                });
            }
            $success = true;
        } catch (\Throwable $exception) {
            app(LoggerInterface::class)->error($exception->getMessage(), ['exception' => $exception, 'act' => '创建停车车道开闸记录分表']);
        } finally {
            try {
                $success && ArchiveCatalog::insertOrIgnore(['name' => $tableName]);
            } catch (\Throwable $exception) {
                app(LoggerInterface::class)->error($exception->getMessage(), ['exception' => $exception, 'act' => '更新存档菜单表']);
            }
        }

        return $success;
    }
}
